import _thread
from datetime import datetime
from time import sleep
from .abc import OS, Notifier

from ..entity import Log
from ..config import config
from .abc import OS, Notifier, TimeLogger
import psutil
import appscript
from AppKit import NSWorkspace
from PIL import Image, ImageGrab


class MacTimeLogger(TimeLogger):
    def __init__(self) -> None:
        self._save_path = config.capture_path
    
    @property
    def capture_save_path(self) -> str:
        return self._save_path
    
    def get_log(self) -> Log:
        log = Log()
        log.time = datetime.now()

        # 获取最前活动窗口的应用程序名
        log.program = NSWorkspace.sharedWorkspace().activeApplication()['NSApplicationName']
        log.title = appscript.app('System Events').application_processes[appscript.its.frontmost == True].windows[1].name.get()[0]
        return log
    
    def save_current_capture(self) -> str:
        img = ImageGrab.grab()
        img_path = self._save_path + datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + '.png'
        img = img.resize((int(img.width * config.capture_scale), int(img.height * config.capture_scale)))
        img.save(img_path)
        return img_path

class MacNotifier(Notifier):
    icon_path = None

    def __init__(self) -> None:
        print('create a MacNotifier!')

    def notify_by_toast(self, msg: str, title: str, duration_second: int) -> None:
        raise NotImplementedError()

    def notify_by_window(self, msg: str, title: str, duration_second: int) -> None:
        raise NotImplementedError()
    

class MacOS(OS):
    def __init__(self) -> None:
        self._notifier = MacNotifier()
        self._time_logger = MacTimeLogger()

    @property
    def notifier(self) -> Notifier:
        return self._notifier
    
    @property
    def time_logger(self) -> TimeLogger:
        return self._time_logger